package com.sky.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TODO
 *
 * @Author Yjr
 * @Date 2025/5/23 20:59
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper,ShoppingCart> implements ShoppingCartService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();

        //判断添加的是菜品还是套餐
        if (shoppingCartDTO.getSetmealId()!=null){
            ShoppingCart shoppingCart = this.lambdaQuery()
                    .eq(ShoppingCart::getUserId, userId)
                    .eq(ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                    .one();
            if (shoppingCart==null){
                shoppingCart=new ShoppingCart();
                Setmeal setmeal = setmealService.getById(shoppingCartDTO.getSetmealId());
                BeanUtils.copyProperties(setmeal, shoppingCart);
                shoppingCart.setSetmealId(setmeal.getId());
                shoppingCart.setUserId(userId);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setNumber(1);
                this.save(shoppingCart);

            }else {
                shoppingCart.setNumber(shoppingCart.getNumber()+1);
                this.updateById(shoppingCart);
            }
        }else {
            ShoppingCart shoppingCart = this.lambdaQuery()
                    .eq(ShoppingCart::getUserId, userId)
                    .eq(ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                    .one();
            if (shoppingCart==null){
                shoppingCart=new ShoppingCart();
                Dish dish = dishService.getById(shoppingCartDTO.getDishId());
                BeanUtils.copyProperties(dish, shoppingCart);
                shoppingCart.setDishId(dish.getId());
                shoppingCart.setUserId(userId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setNumber(1);
                this.save(shoppingCart);
            }else {
                shoppingCart.setNumber(shoppingCart.getNumber()+1);
                this.updateById(shoppingCart);
            }
        }
        
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> queryShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        return this.lambdaQuery().eq(ShoppingCart::getUserId, userId).list();

    }

    /**
     * 清空购物车
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        this.remove(Wrappers.<ShoppingCart>lambdaQuery().eq(ShoppingCart::getUserId, userId));

    }
}
